﻿@using Microsoft.EntityFrameworkCore
@inject DialogService DialogService
@inject RBACService RBACService

<RadzenStack>
    <RadzenTemplateForm EditContext="editContext" TItem="Permission" Data="@model" Submit=@OnSubmit InvalidSubmit=@OnInvalidSubmit>
        <RadzenStack Gap="1rem">
            <RadzenStack class="rz-p-0" Orientation="Orientation.Vertical" AlignItems="AlignItems.Normal" JustifyContent="JustifyContent.SpaceBetween">
                <RadzenRow Gap="1rem">
                    <RadzenColumn Size="12" SizeMD="12">
                        <RadzenStack>
                            <RadzenFormField Text="权限名称" Style="width: 100%;">
                                <ChildContent>
                                    <RadzenTextBox Name="Name" @bind-Value="@model!.UniqueName" />
                                    <RadzenRequiredValidator Component="Name" Text="权限名称不能为空。" Popup=true Style="position: absolute" />
                                </ChildContent>
                                <Helper>
                                    <RadzenText TextStyle="TextStyle.Caption" Style="color:red">* 必填</RadzenText>
                                </Helper>
                            </RadzenFormField>
                        </RadzenStack>
                    </RadzenColumn>
                </RadzenRow>
                <RadzenRow Gap="1rem">
                    <RadzenColumn Size="12" SizeMD="12">
                        <RadzenStack>
                            <RadzenFormField Text="唯一编码" Style="width: 100%;">
                                <ChildContent>
                                    <RadzenTextBox Name="Code" @bind-Value="@model!.UniqueCode" />
                                    <RadzenRequiredValidator Component="Code" Text="唯一编码不能为空。" Popup=true Style="position: absolute" />
                                </ChildContent>
                                <Helper>
                                    <RadzenText TextStyle="TextStyle.Caption" Style="color:red">* 必填</RadzenText>
                                </Helper>
                            </RadzenFormField>
                        </RadzenStack>
                    </RadzenColumn>
                </RadzenRow>
            </RadzenStack>
            <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20%;">
                <RadzenButton ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel" />
                <RadzenButton ButtonStyle="ButtonStyle.Primary" Text="保存" ButtonType="ButtonType.Submit" />
            </RadzenStack>
        </RadzenStack>
    </RadzenTemplateForm>
</RadzenStack>

@code {
    [Parameter] 
    public Permission PPermission { get; set; } = default!;

    private EditContext? editContext;
    private Permission? model = default!;

    void OnSubmit()
    {
        try
        {
            var result = RBACService.SaveAuth(model!);
            DialogService.Close(result);
        }
        catch (Exception e)
        {
            DialogService.Alert(e.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
        }
    }

    void OnInvalidSubmit(FormInvalidSubmitEventArgs args)
    {
        foreach (var error in args.Errors)
        {
            Console.WriteLine(error);
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        
        await Task.Run(() =>
        {
            if (PPermission.Id == 0)
            {
                model = new Permission();
                editContext = new EditContext(model);
            }
            else
            {
                model = PPermission;
                editContext = new EditContext(model);
            }
        });
    }

    private void OnCancel()
    {
        RBACService.ResetOriginalData();
        DialogService.Close(null);
    }

}
